package com.romotc.qqrobot.webqq.qqInterfaceCatching;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.bean.SendMsg;
import com.romotc.qqrobot.bean.SendMsgService;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.qqrobot.webqq.bean.Send_buddy_msg2_RequestBody;
import com.romotc.scache.RequestSpy;
import com.romotc.scache.WebConnection;

public class Send_buddy_msg2_Msg_id_Catching extends QQUrlBodyCatching {
//
//	@Override
//	public boolean modifyResponse(WebConnection wc, RequestSpy rqSpy)
//			throws IOException {
//		return false;
//		
//	}
//
//	/**
//	 * 在请求发出之前获取下Msg_id
//	 */
//	@Override
//	public boolean hackRequest(RequestSpy requestSpy) {
//		QQLogedin curQQ = getCurQqLogedIn(requestSpy);
//		if(curQQ == null)
//			return true;
//		
//		try {
//			Send_buddy_msg2_RequestBody msg = Send_buddy_msg2_RequestBody.parseFromRequestBody(requestSpy.getReqBody());
//			if(msg != null) {
//				int curReqMsgId = msg.getMsgId();
//				//如果当前的请求MsgId大于之前记录的就重新设置一下
//				if(curReqMsgId > curQQ.getMsg_id()) {
//					curQQ.setMsg_id(curReqMsgId);
//				}
//				
//				//虚拟工号发消息时拦截住 保存到数据库中
//				if(WebQQConstant.isRobotUin(curQQ.getLoginUserName())) {
//					SendMsgService.save2Db(msg);
//					return false;
//				}
//			}
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		return true;
//	}

	@Override
	String getHackReqBodyStr(RequestSpy requestSpy, Map<String, Object> var) {
		QQLogedin curQQ = getCurQqLogedIn(requestSpy);
		if(curQQ == null)
			return null;
		
		try {
			Send_buddy_msg2_RequestBody msg = Send_buddy_msg2_RequestBody.parseFromRequestBody(requestSpy.getReqBody());
			if(msg != null) {
				int curReqMsgId = msg.getMsgId();
				//如果当前的请求MsgId大于之前记录的就重新设置一下
				if(curReqMsgId > curQQ.getMsg_id()) {
					curQQ.setMsg_id(curReqMsgId);
				}
				
				//虚拟工号发消息时拦截住 保存到数据库中
				if(WebQQConstant.isRobotUin(curQQ.getLoginUserName())) {
					SendMsgService.save2Db(msg);
					return "{\"retcode\":0,\"result\":\"ok\"}";
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	ArrayList<String> hackRequestHeader(RequestSpy requestSpy, int bodySize,
			Map<String, Object> var) {
//		Cache-Control:no-cache
//		Connection:close
//		Content-Length:29
//		Content-Type:text/plain; charset=utf-8
//		Date:Sun, 19 Feb 2012 22:15:48 GMT
		ArrayList<String> headers = new ArrayList<String>();
		headers.add("Cache-Control:no-cache");
		headers.add("Connection:close");
		headers.add("Content-Length:" + bodySize);
		headers.add("Date:Wed, 25 Jan 2012 03:35:07 GMT");
		headers.add("Content-Type:text/plain; charset=utf-8");
		headers.add("dataFrom: Send_buddy_msg2_Msg_id_Catching");
		return headers;
	}

	@Override
	boolean isGoOn(RequestSpy requestSpy) {
		return false;
	}

	@Override
	public boolean modifyResponse(WebConnection wc, RequestSpy rqSpy)
			throws IOException {
		// TODO Auto-generated method stub
		return false;
	}

}
